
var util = require('../../utils/util.js')
var formatLocation = util.formatLocation


class ChooseCity {

  constructor(pageContext) {
    this.page = pageContext;
    this.page.startSearchInput = this.startSearchInput.bind(this)
    this.page.changeCityName = this.changeCityName.bind(this)
    this.page.getAppLoction = this.getAppLoction.bind(this)
    this.page.appLoctionCB = this.appLoctionCB.bind(this)
    this.page.regexAddress = this.regexAddress.bind(this)
  }

  getAppLoction() {
    var app = getApp()
    let self = this.page
    let currentLocName = getApp().globalData.currentLocName

    if (currentLocName == '出发地') {
      app.appGetLocation(self.getAppLoction)
    }
    let markers = self.data.markers
    markers[0].latitude = getApp().globalData.latitude
    markers[0].longitude = getApp().globalData.longitude
    self.setData({ depName: currentLocName, markers: markers })

    console.log("marksss", markers, currentLocName)
  }



  appLoctionCB() {

    let self = this.page
    let currentLocName = getApp().globalData.currentLocName
    let markers = self.data.markers

    if (markers.length > 1) {
     
      var model = markers[1]
      // console.log('markersss', markers, markers[1], model)
      model.latitude = getApp().globalData.arrLatitude
      model.longitude = getApp().globalData.arrLongitude
    } else {

      let markDict = {
        iconPath: "/images/driverView/arrCityIcon.png",
        id: 0,
        latitude: getApp().globalData.arrLatitude,
        longitude: getApp().globalData.arrLongitude,
        width: 30,
        height: 30
      }

      markers.push(markDict)

    }
    self.getAppLoction()

    self.setData({ markers: markers })
  }

  regexAddress(res, address, btnName){
    let that = this.page
    var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
    var REGION_PROVINCE = [];
    
    
    var addressBean = {
      REGION_PROVINCE: null,
      REGION_COUNTRY: null,
      REGION_CITY: null,
      ADDRESS: null
    };
    function regexAddressBean(address, addressBean, btnName) {
      regex = /^(.*?[市州]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
      console.log("addxress0", res, address)
      var addxress = regex.exec(address);
      console.log("addxress1", addxress)

      if (addxress){
        addressBean.REGION_CITY = addxress[1];

        addressBean.REGION_COUNTRY = addxress[2];
        addressBean.ADDRESS = addxress[3] + "(" + res.name + ")";
      }else{
        if (btnName == "dep") {
          that.setData({ depName: '定位失败,请重试...' });
        } else {
          that.setData({ arrvieName: '定位失败,请重试...' });
        }
      }
      
    }
    if (!(REGION_PROVINCE = regex.exec(res.address)) ) {
      regex = /^(.*?(省|自治区))(.*?)$/;
      REGION_PROVINCE = regex.exec(res.address);
      console.log("REGION_PROVINCE1", REGION_PROVINCE)
      addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
      regexAddressBean(REGION_PROVINCE[3], addressBean);
    } else {
      console.log("REGION_PROVINCE2", REGION_PROVINCE)
      addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
      regexAddressBean(res.address, addressBean);
    }
    that.setData({
      ADDRESS_1_STR:
      addressBean.REGION_PROVINCE + " "
      + addressBean.REGION_CITY + ""
      + addressBean.REGION_COUNTRY
    });
    
    var depAddBean = addressBean
    var arrAddBean = addressBean
    if (btnName == "dep") {
      console.log('zhengze', depAddBean)
      that.setData({ depAddBean: depAddBean});  
    }else{
      console.log('zhengze', arrAddBean)
      that.setData({ arrAddBean: arrAddBean});
    }
    // that.setData(addressBean);  
  }

  startSearchInput(res, cb) {
    let self = this.page
    let app = getApp()
    let btnName = res.currentTarget.dataset.btnname

    wx.chooseLocation({

      success: function (res) {
        console.log(res)
        self.setData({
          hasChangeMap: false,
        })
        let sendAddress = res.address
        var chooseName = res.name
        if (btnName == "dep") {
          app.globalData.latitude = res.latitude;
          app.globalData.longitude = res.longitude;
          app.globalData.currentLocName = chooseName
          self.setData({
            depName: chooseName,
            hasChooseDepName:true,
          })
        } else {
          app.globalData.arrLatitude = res.latitude;
          app.globalData.arrLongitude = res.longitude;

          self.appLoctionCB()

          self.setData({
            arrvieName: chooseName,
          })
        }
        self.regexAddress(res, sendAddress, btnName)
        self.setData({
          hasChangeMap: true,
          hasLocation: true,
          location: formatLocation(res.longitude, res.latitude),
          locationAddress: res.address
        })
      },
      complete: function (res) {
        self.setData({
          hasChangeMap: true,
        })
      }
    })
  }

  changeCityName(res) {
    let self = this.page
    var origDepName = self.data.depName
    var origArrName = self.data.arrvieName
    if (origDepName == "出发地" && origArrName == "目的地") {
      return
    } else if (origDepName == "出发地" && origArrName != "目的地") {
      var tempName = self.data.arrvieName
      self.setData({ depName: tempName, arrvieName: "目的地" })

    } else if (origDepName != "出发地" && origArrName == "目的地") {
      var tempName = self.data.depName
      self.setData({ depName: "出发地", arrvieName: tempName })
    } else {

      var tempName = self.data.depName
      let depName = self.data.arrvieName
      let arrvieName = tempName
      self.setData({ depName: depName, arrvieName: arrvieName })
    }
  }

}


module.exports = ChooseCity;